package com.androidtest3d.view;

import com.androidtest3d.math.MyVector;
import com.androidtest3d.rendering.Renderer3D;
import com.androidtest3d.scene.Node;
import com.androidtest3d.scene.TriangleMesh;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;

/**
 * Android Game Tutorial Project.
 *
 * Copyright (C) 2011  Philipp Jenke
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * This class represents a view for 3D content.
 */
public class View3D extends GLSurfaceView {

	/**
	 * Renderer object
	 */
	private final Renderer3D renderer3d;

	/**
	 * Last coordinates of the mouse
	 */
	private MyVector lastMouseCoordinates = MyVector.makeVector2(-1, -1);

	/**
	 * Camera object.
	 */
	private Camera camera = new Camera();

	/**
	 * Abstraction of the camera control
	 */
	private CameraController cameraController = new CameraController(camera);

	/**
	 * Constructor
	 * 
	 * @param context
	 */
	public View3D(Context context) {
		super(context);

		renderer3d = new Renderer3D(camera);
		setRenderer(renderer3d);
	}

	/*
	 * Event-Handler: user moved the finger over the view. (non-Javadoc)
	 * 
	 * @see android.view.View#onTouchEvent(android.view.MotionEvent)
	 */
	public boolean onTouchEvent(final MotionEvent event) {

		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			lastMouseCoordinates.set(0, event.getX());
			lastMouseCoordinates.set(1, event.getY());
		} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
			if ((lastMouseCoordinates.get(0) > 0)
					&& (lastMouseCoordinates.get(1) > 0)) {
				cameraController
						.mouseDeltaX((float) (event.getX() - lastMouseCoordinates
								.get(0)));
				cameraController
						.mouseDeltaY((float) (event.getY() - lastMouseCoordinates
								.get(1)));
			}
			lastMouseCoordinates.set(0, event.getX());
			lastMouseCoordinates.set(1, event.getY());
		} else if (event.getAction() == MotionEvent.ACTION_UP) {
			lastMouseCoordinates = MyVector.makeVector2(event.getX(),
					event.getY());
		}
		return true;
	}

	/**
	 * Setter.
	 * 
	 * @param scene
	 */
	public void setScene(Node scene) {
		final Node rootNode = new Node();
		final TriangleMesh triangleMesh = new TriangleMesh();
		rootNode.addChild(triangleMesh);
		renderer3d.setScene(rootNode);
	}
}
